﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Common.Extensions
{
// ReSharper disable InconsistentNaming
    public static class IEnumerableExtensions
// ReSharper restore InconsistentNaming
    {
        public static void Each<T>(this IEnumerable<T> items, Action<T, int> action)
        {
            var i = 0;
            foreach (var item in items)
            {
                action(item, i++);
            }
        }

        public static string AggregateToString<T>(this IEnumerable<T> items)
        {
            return AggregateToString(items, String.Empty);
        }

        public static string AggregateToString<T>(this IEnumerable<T> items, string delimiter)
        {
            StringBuilder stringBuilder = new StringBuilder();
            foreach (T item in items)
            {
                stringBuilder.Append(delimiter).Append(item);
            }
            //remove first delimiter
            if(stringBuilder.Length > 0)
            {
                stringBuilder.Remove(0, delimiter.Length);
            }
            return stringBuilder.ToString();
        }
    }
}
